<?php 
include('./fun.php');include('./hd_fun.php');
$conn=my_sql($cfg);
$re=user_login();
if($re['err']=='no_login'){direct('login.php?t='.time());exit;}
if($re['err']!='ok'){die($re['err']);}
$user=$re['user'];
$shop_id=$re['shop_id'];
$shop=get_shop();
$set=get_set();

$sql='select * from '.$cfg['e'].'hd where shop_id="'.$shop['id'].'" ';

$state=g('state');
if($state){
if($state=='stop'){$ss=0;}else{$ss=1;}
$sql.=' and state="'.$ss.'" ';
}

$key=g('key');
if($key){$sql.=' and temp like "%'.a($key).'%" ';}

$page=g('page');if(!$page){$page=1;}
$page_num=10;$start=($page-1)*$page_num;
$sql.=' order by id desc limit '.$start.','.$page_num;
$l=db($sql,$p);



?>
<style>
hd_list img{width:80px;}
hd_list p{padding:0;margin:0}

</style>

<hh><b>团购活动列表</b></hh>
<mm>
<index><a class="m b" href="?url=hd_edit&left_c=hd_list&ac=creat_new">新活动</a>
<?php 
$url=url_get(['page','state']);
if($state){
echo '<a href="?'.$url.'">全部</a>';
}
echo '<a class="'.($state==1?'select':'').'" href="?'.$url.'&state=1">已上架</a>';
echo '<a class="'.($state=='stop'?'select':'').'" href="?'.$url.'&state=stop">未上架</a>';
?>
<input placeholder="请输入关键词" style="box-shadow:none" id="key" value="<?php echo $key;?>"><a onclick="search_it($(this))">搜索</a>
</index>

<hd_list>
<?php 
if($l){
echo '<table>';
th(['图片','活动名称','特色','参与情况','操作']);
foreach($l as $v){
$img=get_img($v['img'],'_240');
if(!$img){$img=$cfg['no_img'];}
echo '<tr>';
echo '<td style="width:100px"><img src="'.$img.'" onclick="select_img($(this))" i="'.$v['id'].'" img="'.$v['img'].'"></td>';
echo '<td style="width:45%">
<p><a href="?url=hd_edit&left_c=hd_list&id='.$v['id'].'" target="_blank">'.base64($v['title']).'</a><p>
<p>开始:'.date('Y-m-d H:i',$v['start']).' 截止:'.date('Y-m-d H:i',$v['end']).'</p>
</td>';
echo '<td>
<p>秒杀:'.(($v['miao_price'] and $v['miao_pro'])?'￥'.$v['miao_price'] .' '.$v['miao_title']:'未开启').'</p>
<p>满赠:'.(($v['mz_price'] and $v['mz_pro'])?$v['mz_title']:'未开启').'</p>
</td>';


echo '<td>
<p>参与:'.$v['people_num'].'</p>
<p>订单:'.$v['order_num'].'</p>
<p>浏览:'.$v['view'].'</p>
</td>';

echo '<td>
'.($v['state']==1?'<a onclick="state($(this))" val="stop" i="'.$v['id'].'">停止</a>':'<a onclick="del($(this))"  i="'.$v['id'].'">删除</a><a onclick="state($(this))" val="1" i="'.$v['id'].'">上线</a>').'
<a href="?url=hd_edit&left_c=hd_list&id='.$v['id'].'" target="_blank">修改</a>

</td>';

echo '</tr>';

}
echo '</table>';
page();
}else{no_data('您还没有新团购活动，点击进行创建');}





?>
</hd_list>

</mm>




<script>
function search_it(o){
var url='<?php echo url_get(['page','key','state']);?>';
var key=$('#key').val();if(!key){key='';}
location.href='?'+url+'&key='+key;
}

function state(o){
var id=o.attr('i');if(!id){msg('请指定活动');return;}
var val=o.attr('val');if(!val){val='stop';}
if(val=='stop'){
if(!confirm("停止活动将会导致用户找不到该活动内容，是否继续?")){return;}
}
post('hd_state',{state:val},id,function(res){
main_load();
});
}

function del(o){
var id=o.attr('i');if(!id){msg('请指定活动');return;}
if(!confirm("是否删除该活动，如果活动已经存在订单，将无法删除，是否继续?")){return;}
post('hd_del','',id,function(res){
    main_load();
    msg('删除成功');
})
}

function change_val(o){
var id=o.attr('i');if(!id){msg('请指定活动');return;}

var c=o.attr('c');
var x=o.attr('x');
if(x=='a'){
var val=o.attr('val');
}else{
var val=o.val();
}

if(val && c){
post('hd_change',{[c]:val},id,function(res){
main_load();
});
}
}

</script>